/**
 * Notes: 收藏业务逻辑 (新版API)
 * Ver : WeCollege Server API 1.0.0
 * Date: 2025-01-27
 * Description: 使用自建服务器API重构收藏业务逻辑
 */

const BaseBiz = require('./base_biz.js');
const serverHelper = require('../../helper/server_helper.js');
const PassportBiz = require('./passport_biz.js');

class FavBiz extends BaseBiz {

  /**
   * 检查是否收藏
   * @param {object} that - 页面this对象
   * @param {string} oid - 对象ID
   * @param {string} type - 类型
   */
  static async isFav(that, oid, type) {
    if (!PassportBiz.isLogin()) {
      that.setData({
        isFav: false
      });
      return;
    }

    try {
      const result = await serverHelper.callServerSumbit('favorite/check', {
        objectId: oid,
        type: type
      }, { title: 'bar' });

      that.setData({
        isFav: result.data && result.data.isFav === true
      });
    } catch (err) {
      console.error('检查收藏状态失败:', err);
      that.setData({
        isFav: false
      });
    }
  }

  /**
   * 切换收藏状态
   * @param {object} that - 页面this对象
   * @param {string} oid - 对象ID
   * @param {string} type - 类型
   * @param {string} title - 标题
   * @returns {Promise<boolean>} 收藏状态
   */
  static async update(that, oid, type, title = '') {
    if (!PassportBiz.isLogin()) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return false;
    }

    let opts = {
      title: '处理中'
    };

    try {
      const result = await serverHelper.callServerSumbit('favorite/toggle', {
        objectId: oid,
        type: type,
        title: title
      }, opts);

      const isFav = result.data && result.data.isFav === 1;
      
      that.setData({
        isFav: isFav
      });

      // 显示提示
      wx.showToast({
        title: isFav ? '收藏成功' : '取消收藏',
        icon: 'success',
        duration: 1500
      });

      return isFav;
    } catch (err) {
      console.error('收藏操作失败:', err);
      wx.showToast({
        title: '操作失败',
        icon: 'none'
      });
      return false;
    }
  }

  /**
   * 删除收藏
   * @param {string} oid - 对象ID
   * @param {string} type - 类型
   * @returns {Promise<boolean>} 删除结果
   */
  static async delete(oid, type) {
    if (!PassportBiz.isLogin()) {
      return false;
    }

    try {
      await serverHelper.callServerSumbit('favorite/delete', {
        objectId: oid,
        type: type
      });

      return true;
    } catch (err) {
      console.error('删除收藏失败:', err);
      return false;
    }
  }

  /**
   * 获取我的收藏列表
   * @param {string} type - 类型
   * @param {object} params - 查询参数
   * @returns {Promise<object>} 收藏列表
   */
  static async getMyFavList(type, params = {}) {
    if (!PassportBiz.isLogin()) {
      return {
        list: [],
        total: 0,
        page: 1,
        size: 20
      };
    }

    try {
      const query = {
        type: type,
        pageNo: params.page || 1,
        pageSize: params.size || 20,
        ...params
      };

      const result = await serverHelper.callServerData('favorite/myList', query);
      
      return {
        list: result.list || [],
        total: result.total || 0,
        page: result.pageNo || query.pageNo,
        size: result.pageSize || query.pageSize,
        count: result.pages || Math.ceil((result.total || 0) / query.pageSize)
      };
    } catch (err) {
      console.error('获取收藏列表失败:', err);
      return {
        list: [],
        total: 0,
        page: 1,
        size: 20
      };
    }
  }

  /**
   * 批量删除收藏
   * @param {Array<string>} oids - 对象ID列表
   * @param {string} type - 类型
   * @returns {Promise<boolean>} 删除结果
   */
  static async batchDelete(oids, type) {
    if (!PassportBiz.isLogin() || !oids || oids.length === 0) {
      return false;
    }

    try {
      await serverHelper.callServerSumbit('favorite/batchDelete', {
        objectIds: oids,
        type: type
      });

      return true;
    } catch (err) {
      console.error('批量删除收藏失败:', err);
      return false;
    }
  }

  /**
   * 获取收藏统计
   * @returns {Promise<object>} 收藏统计
   */
  static async getFavStats() {
    if (!PassportBiz.isLogin()) {
      return {};
    }

    try {
      const result = await serverHelper.callServerData('favorite/stats', {});
      return result || {};
    } catch (err) {
      console.error('获取收藏统计失败:', err);
      return {};
    }
  }

  /**
   * 检查多个对象的收藏状态
   * @param {Array<string>} oids - 对象ID列表
   * @param {string} type - 类型
   * @returns {Promise<object>} 收藏状态映射
   */
  static async batchCheckFav(oids, type) {
    if (!PassportBiz.isLogin() || !oids || oids.length === 0) {
      return {};
    }

    try {
      const result = await serverHelper.callServerData('favorite/batchCheck', {
        objectIds: oids,
        type: type
      });

      return result.favMap || {};
    } catch (err) {
      console.error('批量检查收藏状态失败:', err);
      return {};
    }
  }
}

module.exports = FavBiz;